Imports System.Xml.Serialization
Imports System.IO

Public Class frmComputerAttributeCleanup

    Private myCleanupList As AttributeCleanupList
    Private _FullAttributeCleanupResultDetailList As AttributeCleanupResultDetailList

    Private Sub frm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed

        ' Insert code to set properties and fields of the object.
        Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(AttributeCleanupList))
        ' To write to a file, create a StreamWriter object.
        Dim myWriter As StreamWriter = New StreamWriter(My.Settings.ComputerManagementAttributeCleanupSetPath)
        mySerializer.Serialize(myWriter, myCleanupList)
        myWriter.Close()

    End Sub

    Private Sub frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        If File.Exists(My.Settings.ComputerManagementAttributeCleanupSetPath) Then
            ' Construct an instance of the XmlSerializer with the type
            ' of object that is being deserialized.
            Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(AttributeCleanupList))
            ' To read the file, create a FileStream.
            Dim fs As FileStream = New FileStream(My.Settings.ComputerManagementAttributeCleanupSetPath, FileMode.Open)
            ' Call the Deserialize method and cast to the object type.
            myCleanupList = CType(mySerializer.Deserialize(fs), AttributeCleanupList)

            fs.Close()
        Else
            myCleanupList = New AttributeCleanupList
        End If

        Me.AttributeCleanupListBindingSource.DataSource = myCleanupList
        ToolStripStatusLabel1.Text = myCleanupList.Count & " clean up sets"

    End Sub

    Private Sub btnRunProcess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRunProcess.Click

        Dim warning As String = "Are you sure you want to process all the cleanup sets against the group: " & My.Settings.ComputerManagementTargetGroupCN & "?"

        If MessageBox.Show(warning, Application.ProductName, MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            ProcessCleans(False)

            'serialize the old and new values list in case it needs to be repaired manually
            Dim logFileName As String = System.IO.Path.GetTempPath & "AttributeCleanupLog" & DateTime.Now.ToFileTime & ".txt"

            ' Insert code to set properties and fields of the object.
            Dim mySerializer As XmlSerializer = New XmlSerializer(GetType(AttributeCleanupResultDetailList))
            ' To write to a file, create a StreamWriter object.
            Dim myWriter As StreamWriter = New StreamWriter(logFileName)
            mySerializer.Serialize(myWriter, _FullAttributeCleanupResultDetailList)
            myWriter.Close()

            MessageBox.Show("Results have been exported to " & logFileName & " for use in any manual archiving, etc.")
        End If

    End Sub

    Private Sub btnEvaluate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEvaluate.Click

        ProcessCleans(True)

    End Sub

    Private Sub dgvCleanupResults_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgvCleanupResults.CurrentCellChanged

        'filter the data source of the details by the values in this
        Dim dgv As DataGridView = CType(sender, DataGridView)
        If Not dgv Is Nothing Then
            If Not dgv.CurrentCell Is Nothing Then
                Dim i As Integer = dgv.CurrentCell.RowIndex

                Dim acs As AttributeCleanupSet = myCleanupList(i)

                If Not Me._FullAttributeCleanupResultDetailList Is Nothing Then
                    Dim acrdList As AttributeCleanupResultDetailList = Me._FullAttributeCleanupResultDetailList.FindAll(acs)

                    Me.AttributeCleanupResultDetailListBindingSource.DataSource = acrdList
                End If
            End If

        End If

    End Sub

    Private Sub ProcessCleans(ByVal IsEvaluationOnly As Boolean)

        Dim ac As AttributeCleanup = New AttributeCleanup

        'evaluate the results of making the cleans, but don't actually apply
        Dim acrl As AttributeCleanupResultList = ac.ProcessCleans(myCleanupList, IsEvaluationOnly)
        Me.AttributeCleanupResultListBindingSource.DataSource = acrl

        'store it at the module level, for further filtering
        _FullAttributeCleanupResultDetailList = ac.AttributeCleanupResultDetails

        Me.AttributeCleanupResultDetailListBindingSource.DataSource = _FullAttributeCleanupResultDetailList

        ToolStripStatusLabel1.Text = acrl.Count & " clean up results"

    End Sub

End Class